const domainName = getApp().globalData.domainName
const parameter = getApp().globalData.parameter

const page_init=(that,jg_id)=>{
    wx.request({
      url: domainName + '/add_paper_init',
      data: {
        jg_id:jg_id,
      },
      ...parameter,
      success: res => {
        that.setData({
          chap_list:res.data.chap_list,
          stu_label_list:res.data.label_list,
        })
      },
    })
  }
  // 加载我的学生
const load_students=(that,jg_id,label_id_list,stu_status_id_list, stu_exam_id_list)=>{
  wx.showLoading({
    title: '加载学员中……',
  })
  wx.request({
    url: domainName + '/load_students_for_test_paper',
    data: {
      jg_id:jg_id,
      label_id_list:JSON.stringify(label_id_list||[]),
      status_id:JSON.stringify(stu_status_id_list||[]),
      exam_id_list:JSON.stringify(stu_exam_id_list||[]),
    },
    ...parameter,
    success: res => {
      let stu_list = res.data.stu_list
      if(stu_list.length==0){
        wx.showToast({
          title: '这里没有学员',
          icon: 'error',
        })
      }
      let template_data = that.data.template_data
      template_data.stu_list = stu_list
      that.setData({
        template_data:template_data,
        stu_list:res.data.stu_list,
      })
      wx.hideLoading()
    },
  })
}
const submit=(formData)=>{
  wx.request({  // 提交数据
    url: domainName + '/add_paper_submit',
    data: {
      formData:JSON.stringify(formData)
    },
    ...parameter,
    success: res => {
      wx.hideLoading()
      let test_paper_id = res.data.test_paper_id
      if(test_paper_id){


        // 给上一页信号（can_onShow）
        var pages = getCurrentPages()
        var prePage = pages[pages.length-2]
        prePage.can_onShow = true
    

        wx.showModal({
          showCancel: false,
          title: '创建成功',
          success: res => {
            wx.redirectTo({
              url: '/pages/doTi/paper_details/paper_details?test_paper_id=' + test_paper_id,
            })
          },
        })
      }
    },
  })
}

// 打包表单数据
const package_formData=(that)=>{
  return {
    'jg_id':that.jg_id,
    'start_date':that.data.start_date,
    'start_time':that.data.start_time,
    'test_type':1,
    'jg_stu_id_list':that.data.jg_stu_id_list,
    'chap_id_list':that.data.chap_id_list,
    'test_duration':that.data.test_duration,
    'capacity':that.data.capacity,
    'invalid_date':that.data.invalid_date,
    'invalid_time':that.data.invalid_time,
    'test_paper_name':that.data.test_paper_name,
  }
}
// 检查表单的数据
const check_formData=(formData)=>{
  if(!formData.test_paper_name){
    wx.showModal({
      content: '请填入试卷名称',
      showCancel: false,
      title: '提示',
    })
    return
  }
  if(!formData.jg_stu_id_list.length){
    wx.showModal({
      content: '请选择学员',
      showCancel: false,
      title: '提示',
    })
    return
  }
  if(!formData.chap_id_list.length){
    wx.showModal({
      content: '请选择章节',
      showCancel: false,
      title: '提示',
    })
    return
  }
  return true
}
// 筛选学员状态
const select_stu_exam=(that, e)=>{
  let stu_exam_id = parseInt(e.currentTarget.id)
  let stu_exam_id_list = that.data.stu_exam_id_list
  let index = stu_exam_id_list.indexOf(stu_exam_id)
  if(index>-1){
    stu_exam_id_list.splice(index,1)
  }else{
    stu_exam_id_list.push(stu_exam_id)
  }
  that.setData({
    stu_exam_id_list,
    jg_stu_id_list:[],
  })
  load_students(that,that.jg_id,that.data.selected_label_list,that.data.stu_status_id_list,stu_exam_id_list)
}


// 筛选学员状态
const select_stu_status=(that, e)=>{
  let stu_status_id = parseInt(e.currentTarget.id)
  let stu_status_id_list = that.data.stu_status_id_list
  let index = stu_status_id_list.indexOf(stu_status_id)
  if(index>-1){
    stu_status_id_list.splice(index,1)
  }else{
    stu_status_id_list.push(stu_status_id)
  }
  that.setData({
    stu_status_id_list,
    jg_stu_id_list:[],
  })
  load_students(that,that.jg_id,that.data.selected_label_list,stu_status_id_list,that.data.stu_exam_id_list)
}

//  选择学员范围
const selected_jgStuIdList=(that,e)=>{
  that.setData({
    jg_stu_id_list:e.detail.value
    })
}
const select_stu=(that,e)=>{
    let index = e.currentTarget.id
    let template_data = that.data.template_data
    template_data.stu_list[index].checked = !template_data.stu_list[index].checked
    that.setData({
      template_data:template_data,
    })
}
const cancel_stu=(that)=>{
    let template_data = that.data.template_data
    for(var i=0;i<template_data.stu_list.length;i++){
      template_data.stu_list[i].checked = false
    }   

    that.setData({
      template_data:template_data,
      jg_stu_id_list:[],
    })
}
// 选择全
const all_stu=(that,e)=>{
  let template_data = that.data.template_data
  let jg_stu_id_list = []
  for(var i=0;i<template_data.stu_list.length;i++){
    template_data.stu_list[i].checked = !template_data.stu_list[i].checked
    if(template_data.stu_list[i].checked){
      jg_stu_id_list.push(template_data.stu_list[i].jg_stu_id)
    }
  }      
  that.setData({
    template_data,
    jg_stu_id_list,
  })
}
// 设置标签
const select_label=(that,e)=>{
  let label_id = e.currentTarget.id
  let selected_label_list = that.data.selected_label_list
  let label_id_index = selected_label_list.indexOf(label_id)
  if(label_id_index>-1){
    selected_label_list.splice(label_id_index, 1)
  }else{
    selected_label_list.push(label_id)
  }
  that.setData({
    selected_label_list,
    jg_stu_id_list:[],
  })
  load_students(that,that.jg_id,selected_label_list,that.data.stu_status_id_list, that.data.stu_exam_id_list)
}
//  选择考试范围
const chap_id_list=(that, e)=>{
  // console.log('选择考试范围',e)
  let chap_id_list = e.detail.value
  that.setData({
    chap_id_list:chap_id_list
  })
}
// 选择全部章节
const all_chap=(that,e)=>{
that.get_vibration()
  if(that.data.all_chap){
    that.setData({
      all_chap:false,
      chap_id_list:[]
    })
  }else{
    var chap_id_list = []
    for(var i=0;i<that.data.chap_list.length;i++){
      chap_id_list.push(that.data.chap_list[i].chap_id)
    }
    that.setData({
      all_chap:true,
      chap_id_list:chap_id_list
    })
  }
}
// 设置默认试卷名称
const default_name=(that)=>{
  that.setData({
    test_paper_name:util.formatDateTime_str(new Date()) + '【试卷】'
  })
}
// 输入试卷名称
const test_paper_name=(that,e)=>{
  that.setData({
    test_paper_name:e.detail.value
  })
}
// 设置失效日期
const set_invalid_date=(that,e)=>{
  let invalid_date=e.detail.value;
  if(invalid_date>=that.data.start_date){
    that.setData({
      invalid_date:invalid_date
    })
  };
}
// 设置失效时间
const set_invalid_time=(that,e)=>{
  that.setData({invalid_time:e.detail.value})
}
// 设置生效时间
const set_start_date=(that,e)=>{
  let value =  e.detail.value;
  that.setData({
    start_date:value,
    invalid_date:util.daysLaterOrAfter(value,1)
  })
}


// 提交创建试卷

const submit_ask=(that, e)=>{
  var formData = package_formData(that)  // 打包数据
  if(!check_formData(formData)){  // 检查数据完整性
    return
  }
  wx.showModal({  // 询问确定提交
    content: '确定发布考试试卷吗？',
    showCancel: true,
    title: '提示',
    success: res => {
      if(res.confirm){
        wx.showLoading()
        submit(formData)
      }
    },
  })
}
  

const util = require('../../../utils/util.js')
const windowWidth = wx.getSystemInfoSync().windowWidth

Page({

  /**
   * 页面的初始数据
   */
  data: {
    template_data : {section_width:windowWidth-60,column_margin:10,column:5,font_size:30,show_status:true,show_checkbox:true, stu_list:[]},
    radio:true,
    time_list:[10,20,30,40,50,60,70,80,90,100,110,120],
    capacity_list:[100, 200, 300, 400,500],
    capacity:100,
    test_duration:30, // 时间
    start_date:util.formatDate(new Date()),
    invalid_date:util.daysLaterOrAfter(new Date(), 1),
    invalid_time:util.formatTime(new Date()).slice(11,-3),
    start_time:util.formatTime(new Date()).slice(11,-3),

    selected_label_list:[],
    stu_status_id_list:[],
    stu_exam_id_list:[],
    jg_stu_id_list:[],
    chap_id_list:[],
  },


  // 使用默认试卷名称
  default_name(){getApp().get_vibration(); default_name(this)},
  // 输入试卷名称
  test_paper_name(e){ test_paper_name(this,e)},
  // 选择失效时间
  set_invalid_date(e){ set_invalid_date(this,e)},
  // 选择失效时间
  set_invalid_time(e){ set_invalid_time(this,e)},
  // 选择题量
  set_capacity(e){this.setData({capacity:this.data.capacity_list[e.detail.value]})},
  // 设置考试时长
  set_test_duration(e){this.setData({test_duration:this.data.time_list[e.detail.value]})},
  // 选择生效时间
  set_start_time(e){this.setData({start_time:e.detail.value})},
  // 设置开始考试日期
  set_start_date(e){ set_start_date(this,e)},
  // 操作章节
  all_chap(e){getApp().get_vibration(), all_chap(this,e)},
  chap_id_list(e){getApp().get_vibration(), chap_id_list(this,e)},



  /**
   * 操作学员
   **/

  // 筛选学员标签
  select_label(e){getApp().get_vibration(), select_label(this,e)},
  // 筛选学员状态
  select_stu_status(e){getApp().get_vibration(), select_stu_status(this,e)},
  // 筛选考试状态
  select_stu_exam(e){getApp().get_vibration(), select_stu_exam(this,e)},
  // 全选学员
  all_stu(e){getApp().get_vibration(), all_stu(this,e)},
  // 选择单个学员
  select_stu(e){getApp().get_vibration(), select_stu(this,e)},
  // 清除选择学员
  cancel_stu(e){getApp().get_vibration(), cancel_stu(this,e)},
  // 获取已选学员ID
  stu_id_list(e){getApp().get_vibration(), selected_jgStuIdList(this,e)},
  

  // 提交创建试卷
  submit_ask(e){getApp().get_vibration(), submit_ask(this,e)},


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log('options',options)
    this.jg_id = options.jg_id
    page_init(this, this.jg_id)
  },


})